package com.lin.lazybone.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.lin.lazybone.R;
import com.lin.lazybone.adapter.WorkListAdapter;
import com.lin.lazybone.application.BaseActivity;
import com.lin.lazybone.entity.Order;
import com.lin.lazybone.entity._User;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * Created by mr.lin on 2017/11/14.
 * 用户抢单后的任务
 */

public class MyWorksActivity extends BaseActivity implements WorkListAdapter.WorkCallback
{

    private ListView wotk_listview;
    private List<Order> mList;
    private WorkListAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myworks);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null)
        {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        initData();
        initview();

    }

    private void initData()
    {
        //从服务器获取由我抢到的订单
        mList = new ArrayList<>();
        getOrderData();

    }

    private void initview()
    {
        wotk_listview = (ListView) findViewById(R.id.wotk_listview);
        adapter = new WorkListAdapter(this, mList, this);
        wotk_listview.setAdapter(adapter);

    }

    public void getOrderData()
    {
        final CustomDialog dialog = new CustomDialog(this, 100, 400, R.layout.dialog_loding,
                R.style.Theme_dialog, Gravity.CENTER,R.style.pop_anim_style);
        dialog.setCancelable(false);
        dialog.show();
        mList.clear();
        //开启新线程来获取数据
        new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                //从服务器上获取订单列表
                BmobQuery<Order> query = new BmobQuery<Order>();
                //返回50条数据，如果不加上这条语句，默认返回10条数据
                query.setLimit(50);
                //查询状态为1，也就是待抢单的订单
                query.addWhereEqualTo("order_uid", BmobUser.getCurrentUser(_User.class).getUsername());
                query.addWhereEqualTo("order_state", 2);
                //执行查询方法
                query.findObjects(new FindListener<Order>()
                {
                    @Override
                    public void done(final List<Order> object, BmobException e)
                    {
                        if (e == null)
                        {
                            mList.addAll(object);
                            runOnUiThread(new Runnable()
                            {
                                @Override
                                public void run()
                                {
                                    Toast.makeText(MyWorksActivity.this, "查询成功：共" + object.size() + "条任务。", Toast.LENGTH_SHORT).show();
                                    dialog.dismiss();
                                    adapter.notifyDataSetChanged();
                                }
                            });
                        } else
                        {
                            runOnUiThread(new Runnable()
                            {
                                @Override
                                public void run()
                                {
                                    Toast.makeText(MyWorksActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
                                    dialog.dismiss();
                                }
                            });
                        }
                    }
                });
            }
        }).start();
    }

    //通过回掉来对点击事件进行处理
    @Override
    public void buttonclick(View v)
    {
        int posotion = (Integer) v.getTag();
        switch (v.getId())
        {
            case R.id.worklist_cancelbt:
                Toast.makeText(this, "cancel", Toast.LENGTH_SHORT).show();
                break;
            case R.id.worklist_chatbt:
//                Toast.makeText(this, "chat", Toast.LENGTH_SHORT).show();
                Order order = mList.get(posotion);
                Intent intent = new Intent(this, ChatActivity.class);
                intent.putExtra("name", order.getOrder_aid());
                intent.putExtra("FromWhere", 1);
                startActivity(intent);
                break;
            case R.id.worklist_donebt:
                Toast.makeText(this, "done", Toast.LENGTH_SHORT).show();
                break;
        }

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
